package com.wh.service.initial;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.mapper.initial.ProductMapper;
import com.wh.mapper.initial.StorehouseMapper;
import com.wh.pojo.Product;
import com.wh.pojo.Storehouse;
import com.wh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private StorehouseMapper storehouseMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult getProductList(PageResult pageResult) {
        Page<Product> productPage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());

        queryWrapper.like(flag,"name",pageResult.getQuery());
        productPage = productMapper.selectPage(productPage,queryWrapper);

        pageResult.setTotal((long) productPage.getRecords().size()).setRows(productPage.getRecords());
//        System.out.println(productPage.getRecords().size());
        return pageResult;
    }

    @Override
    public void addProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void updataProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void deleteProduct(String barCode) {
        Map<String,Object> map = new HashMap<>();
        map.put("bar_code",barCode);
        productMapper.deleteByMap(map);
    }

    @Override
    public List<Storehouse> getStorehouse() {
        List<Storehouse> storehouses = storehouseMapper.selectList(null);
        return storehouses;
    }
}
